#pragma once
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <memory>
#include "logmessage.hpp"

enum types
{
    TCP = 1,
    UDP = 2,
    IP = 4
    //...
};

const int DEF_FD = -1;
const std::string DEF_IP = "0.0.0.0";
const uint16_t DEF_PORT = -1;

class Socket
{
    static const int s_backlog = 20;

    int _sockfd = DEF_FD;
    std::string _lip = DEF_IP;
    uint16_t _lport = DEF_PORT;
    std::string _fip = DEF_IP;
    uint16_t _fport = DEF_PORT;
    int _type;

public:
    Socket(int type = TCP | IP)
    :_type(type)
    {}

    void Create()
    {
        _sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (_sockfd == -1)
        {
            LogMessage(FATAL, "socket(%d): %s", errno, strerror(errno));
            exit(errno);
        }
        LogMessage(DEBUG, "create socket success!");
    }

    void Bind(const std::string &ip, uint16_t port)
    {
        sockaddr_in local;
        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(port);
        local.sin_addr.s_addr = inet_addr(ip.c_str());

        if (bind(_sockfd, (sockaddr *)&local, sizeof(local)) == -1)
        {
            LogMessage(FATAL, "bind(%d): %s", errno, strerror(errno));
            exit(errno);
        }
        _lip = ip;
        _lport = port;
        LogMessage(DEBUG, "bind socket success!");
    }

    void Listen()
    {
        if (listen(_sockfd, s_backlog))
        {
            LogMessage(FATAL, "listen(%d): %s", errno, strerror(errno));
            exit(errno);
        }
        LogMessage(DEBUG, "listen socket success!");
    }

    int Accept(std::shared_ptr<Socket> socket_sptr)
    {
        sockaddr_in src;
        socklen_t len = sizeof(src);
        int svcsock = accept(_sockfd, (sockaddr *)&src, &len);
        if (svcsock == -1) return -1;
        if (socket != nullptr)
        {
            socket_sptr->_sockfd = svcsock;
            socket_sptr->_type = _type;
            socket_sptr->_fip = inet_ntoa(src.sin_addr);
            socket_sptr->_fport = ntohs(src.sin_port);
            socket_sptr->_lip = _lip;
            socket_sptr->_lport = _lport;
        }
        return svcsock;
    }

    void Connect(const std::string &ip, uint16_t port)
    {
        //以下是connect的过程
        sockaddr_in dst;
        memset(&dst, 0, sizeof(dst));
        dst.sin_family = AF_INET;
        dst.sin_port = htons(port);
        dst.sin_addr.s_addr = inet_addr(ip.c_str());
        if (connect(_sockfd, (sockaddr *)&dst, sizeof(dst)) == -1)
        {
            LogMessage(FATAL, "connect(%d): %s", errno, strerror(errno));
            exit(errno);
        }
        // 以下是获取local address的过程
        sockaddr_in local;
        socklen_t len = sizeof(local);
        if(getsockname(_sockfd, (sockaddr*)&local, &len) == -1)
        {
            LogMessage(FATAL, "getsockname(%d): %s", errno, strerror(errno));
            exit(errno);
        }
        _lip = inet_ntoa(local.sin_addr);
        _lport = ntohs(local.sin_port);
        _fip = ip;
        _fport = port;
    }

    ssize_t Receive(void* buffer, size_t bufsz)
    {
        ssize_t sz = recv(_sockfd, buffer, bufsz, 0);
        return sz;
    }

    ssize_t Send(const void* buffer, size_t bufsz)
    {
        ssize_t sz = send(_sockfd, buffer, bufsz, 0);
        return sz;
    }

    int Fd()
    {
        return _sockfd;
    }

    void ModFd(int fd)
    {
        _sockfd = fd;
    }

    const std::string& Fip()
    {
        return _fip;
    }

    const std::string& Lip()
    {
        return _lip;
    }

    uint16_t Fport()
    {
        return _fport;
    }

    uint16_t Lport()
    {
        return _lport;
    }

    ~Socket()
    {
        if (_sockfd >= 0)
        {
            close(_sockfd);
        }
    }
};